package cn.javacc.core.util;

import java.util.ArrayList;
import java.util.List;
/**
 * 
 * @author mzh2008@163.com 2012-11-12 下午10:46:58
 *
 */
public class Hql {
	private String hqlStr;//
	@SuppressWarnings("rawtypes")
	private List params = new ArrayList();

	private Hql(String hqlStr) {
		this.hqlStr = hqlStr;
	}

	public static Hql Start(String sql) {
		return new Hql(sql);
	}

	@SuppressWarnings("unchecked")
	public Hql addWhere(String prifix, Where where) {
		if (where.getSqlStr() == null) {
			return this;
		} else {
			hqlStr = hqlStr + " " + prifix + " " + where.getSqlStr();
			params.addAll(where.getParams());
			return this;
		}
	}

	public Hql forPage(int toPage, int pageSize) {
		if (toPage < 1) {
			toPage = 1;
		}
		int startRow = (toPage - 1) * pageSize;
		hqlStr = hqlStr + " limit " + startRow + "," + pageSize;
		return this;
	}

	public Hql addOrderDesc(String filed) {
		hqlStr = hqlStr + " order by " + filed + " desc ";
		return this;
	}

	public Hql addOrderAsc(String filed) {
		hqlStr = hqlStr + " order by " + filed;
		return this;
	}

	public String getHqlStr() {
		return hqlStr;
	}

	public void setHqlStr(String hqlStr) {
		this.hqlStr = hqlStr;
	}

	@SuppressWarnings("rawtypes")
	public List getParams() {
		return params;
	}

	@SuppressWarnings("rawtypes")
	public void setParams(List params) {
		this.params = params;
	}

}
